package cn.yeyiu.sexyiu.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;

import cn.yeyiu.sexyiu.R;
import cn.yeyiu.sexyiu.task.InitIPTask;
import cn.yeyiu.util.AndroidUtil;
import cn.yeyiu.util.NetConfig;

public class MainActivity extends AppCompatActivity {
    private long exitTime;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav_view);

        NavHostFragment navHostFragment =
                (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
        assert navHostFragment != null;
        NavController navController = navHostFragment.getNavController();

        NavigationUI.setupWithNavController(bottomNavigationView, navController);

        handlerInit();
        serverConnect();
    }

    @Override
    public boolean onKeyDown(int i, KeyEvent keyEvent) {
        if (i == 4 && keyEvent.getAction() == 0) {
            if (System.currentTimeMillis() - this.exitTime > 2000) {
                Toast.makeText(this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
                this.exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
        }
        return false;
    }

    public void handlerInit() {
        AndroidUtil.setMainHandler(new Handler(getMainLooper(), new Handler.Callback() {

            public boolean handleMessage(Message message) {
                switch (message.what){
                    case 1:
                        toast(message.obj.toString());
                        break;
                    case 2:
                        alert(message.obj.toString(), true);
                        break;
                    case 3:
                        alert(message.obj.toString(), false);
                        break;
                    default:break;
                }
                return true;
            }
        }));
    }

    void serverConnect() {
        String oldIp = NetConfig.getOldIp(this);
        if (oldIp != null) {
            NetConfig.setIp(this, oldIp);
        } else {
            new InitIPTask(this).execute();
        }
    }

    void toast(String msg){
        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
    }
    void alert(String msg, boolean isCancelable){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Sexyiu").setMessage(msg).setCancelable(isCancelable);
        builder.show();
    }
}
